﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GGJ.Sprites
{
    class Ennemies
    {
        private Texture2D texture;

        private List<Ennemy> ennemies;

        private Random random;

        private double timeSinceLastGeneration = 0.0d;

        GameplayScreen.Level type;

        public Ennemies(Texture2D texture,GameplayScreen.Level type)
        {
            this.type = type;
            random = new Random();
            this.texture = texture;
            ennemies = new List<Ennemy>();

            AddEnnemy();
        }

        public void AddEnnemy()
        {

            switch (type)
            {
                case GameplayScreen.Level.Kitchen:
                    int r = random.Next(4);

                    switch (r)
                    {
                        case 0:
                            ennemies.Add(new FlyCatcher(texture, 800, 0));
                            break;
                        case 1:
                            ennemies.Add(new FlySwatter(texture, 800, 600));
                            break;
                        case 2:
                            ennemies.Add(new Insecticide(texture, 800, 400));
                            break;
                        case 3:
                            ennemies.Add(new HoneyPot(texture, 800, 400));
                            break;
                       
                    }
                    break;
                case GameplayScreen.Level.Backyard:
                    int r2 = random.Next(3);

                    switch (r2)
                    {
                        case 0:
                            ennemies.Add(new EatingPlant(texture, 800, 400));
                            break;
                        case 1:
                            ennemies.Add(new HoneyPot(texture, 800, 400));
                            break;
                        case 2:
                            ennemies.Add(new FlySwatter(texture, 800, 600));
                            break;
                    }
                    break;
            }

            
        }

        public void Update(GameTime gameTime, Flies flies,KilledFlies killedFlies,bool isSlowMotion)
        {

            float realSpeed = Program.speedEnnemys;

            if (isSlowMotion)
                realSpeed = realSpeed / 2;

            for (int i=0; i < ennemies.Count; i++)
            {
                ennemies[i].Update(gameTime, flies,killedFlies,realSpeed,isSlowMotion);

                if (ennemies[i].isOutOfGame())
                {
                    ennemies.RemoveAt(i);
                }
            }

             timeSinceLastGeneration += gameTime.ElapsedGameTime.TotalSeconds;

             if (timeSinceLastGeneration >= 2.0f)
             {

                 timeSinceLastGeneration = 0.0d;

                 AddEnnemy();
             }

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Ennemy ennemy in ennemies)
            {
                if (ennemy.GetType() == typeof(FlySwatter))
                    ennemy.Draw(spriteBatch);
            }

            foreach (Ennemy ennemy in ennemies)
            {
                if (ennemy.GetType() != typeof(FlySwatter))
                    ennemy.Draw(spriteBatch);
            }
        }

        public int Count()
        {
            return ennemies.Count;
        }

        public Ennemy GetEnnemy(int index)
        {
            return ennemies[index];
        }
    }
}
